/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CriarObjeto.java
 *
 * Created on 12/09/2011, 23:39:28
 */
package visao;

import controle.ControlaTudo;
import controle.Controle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import mod.Aresta;
import mod.Face;
import mod.Objeto;
import mod.Ponto;

/**
 *
 * @author TyTu
 */
public class CriarObjeto extends javax.swing.JFrame {

    /** Creates new form CriarObjeto */
    public CriarObjeto() {
        initComponents();
        jPanel1.setSize(400, 300);
    }
    private Objeto objeto = new Objeto();
    private ArrayList<Ponto> pontos = new ArrayList<Ponto>();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButtonRotacao = new javax.swing.JButton();
        jButtonExtrusao = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jButtonFecharObjeto = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gerando o objeto");
        setAlwaysOnTop(true);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setPreferredSize(new java.awt.Dimension(400, 300));
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanel1MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jButtonRotacao.setBackground(new java.awt.Color(153, 204, 255));
        jButtonRotacao.setText("Rotação");
        jButtonRotacao.setAutoscrolls(true);
        jButtonRotacao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRotacaoActionPerformed(evt);
            }
        });

        jButtonExtrusao.setBackground(new java.awt.Color(153, 255, 153));
        jButtonExtrusao.setText("Extrusão");
        jButtonExtrusao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExtrusaoActionPerformed(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jButtonFecharObjeto.setText("Fechar Objeto");
        jButtonFecharObjeto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFecharObjetoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButtonExtrusao, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonRotacao, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE))
                    .addComponent(jButtonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonFecharObjeto))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonRotacao)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonExtrusao)
                        .addGap(46, 46, 46)
                        .addComponent(jButtonFecharObjeto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 161, Short.MAX_VALUE)
                        .addComponent(jButtonCancelar)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseClicked
        System.out.println("Criar Objeto - ADD Ponto");
        Ponto pontoAtual = new Ponto((double)evt.getX(),(double) evt.getY(), 0);
        //
        if (!objeto.isEmpty()) {
            jPanel1.getGraphics().drawLine((int) pontos.get(pontos.size() - 1).getX(), (int) pontos.get(pontos.size() - 1).getY(), evt.getX(), evt.getY());
//            objeto.addaresta(new Aresta(new Ponto((double) pontos.get(pontos.size() - 1).getX(), (double) pontos.get(pontos.size() - 1).getY(), 0), new Ponto((double)evt.getX(), (double)evt.getY(), (double)0)));
            Ponto pontoUltimo = pontos.get(pontos.size() - 1);
            Aresta novaAresta = new Aresta(pontoUltimo, pontoAtual);
            //
//            objeto.getArestas().add(new Aresta(new Ponto((double) pontos.get(pontos.size() - 1).getX(), (double) pontos.get(pontos.size() - 1).getY(), 0), new Ponto((double)evt.getX(), (double)evt.getY(), (double)0)));
            objeto.getArestas().add(novaAresta);
        }
        pontos.add(pontoAtual);
        objeto.getPontos().add(pontoAtual);
    }//GEN-LAST:event_jPanel1MouseClicked

    private void jButtonExtrusaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExtrusaoActionPerformed
        // TODO add your handling code here:
        if (pontos.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "Não tem pontos!");
        } else {

            String s;
            int z = 360;
            for (;;) {
                try {
                    s = JOptionPane.showInputDialog(rootPane, "Variação em Z ");
                    if (s != null) {
                        z = Integer.parseInt(s);
                    }
                    break;
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(rootPane, "Valor digitado incorreto!");
                    e.printStackTrace();
                }
            }

            if (s != null) {
                objeto.extrusao(z);
                Controle.getCena().addObj(objeto);
                dispose();
            }


        }
    }//GEN-LAST:event_jButtonExtrusaoActionPerformed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jButtonCancelarActionPerformed

private void jButtonRotacaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRotacaoActionPerformed
// TODO add your handling code here:
    if (pontos.isEmpty()) {
        JOptionPane.showMessageDialog(rootPane, "Não tem pontos!");
    } else {

        String s;
        int z = 360;
        for (;;) {
            try {
                s = JOptionPane.showInputDialog(rootPane, "Grid: ");
                if (s != null) {
                    z = Integer.parseInt(s);
                }
                break;
            } catch (Exception e) {
                JOptionPane.showMessageDialog(rootPane, "Valor digitado incorreto!");
                e.printStackTrace();
            }
        }
        if (s != null) {
            objeto.revolucao(z);
            objeto.exibirAresta();
            Controle.getCena().addObj(objeto);
            dispose();
        }



    }

}//GEN-LAST:event_jButtonRotacaoActionPerformed

private void jButtonFecharObjetoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFecharObjetoActionPerformed
    // TODO add your handling code here:
    
    if (!objeto.isEmpty()) {
            jPanel1.getGraphics().drawLine((int) pontos.get(pontos.size() - 1).getX(), (int) pontos.get(pontos.size() - 1).getY(), (int) pontos.get(0).getX(), (int) pontos.get(0).getY());
//            objeto.addaresta(new Aresta(new Ponto((double) pontos.get(pontos.size() - 1).getX(), (double) pontos.get(pontos.size() - 1).getY(), 0), new Ponto( pontos.get(0).getX(),  pontos.get(0).getY(), (double)0)));
//            objeto.getArestas().add(new Aresta(new Ponto((double) pontos.get(pontos.size() - 1).getX(), (double) pontos.get(pontos.size() - 1).getY(), 0), new Ponto( pontos.get(0).getX(),  pontos.get(0).getY(), (double)0)));
            Ponto primeiroPonto = objeto.getPontos().get(0);
            Ponto ultimoPonto = objeto.getPontos().get(objeto.getPontos().size()-1);
            //
            Aresta arestaFecha = new Aresta(ultimoPonto, primeiroPonto);
            objeto.getArestas().add(arestaFecha);
            //
            Face f=new Face();
            for(int i=0;i<objeto.getArestas().size();i++){
//                f.getArestas().add(objeto.getaresta(i));
                f.getArestas().add(objeto.getArestas().get(i));
//            objeto.addface(f);
            }
            objeto.getFaces().add(f);
            
        } else {
           
        }
    
    
}//GEN-LAST:event_jButtonFecharObjetoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CriarObjeto().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonExtrusao;
    private javax.swing.JButton jButtonFecharObjeto;
    private javax.swing.JButton jButtonRotacao;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
